package com.spzx.user.service.impl;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.spzx.common.core.constant.UserConstants;
import com.spzx.common.core.enums.UserStatus;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.utils.DateUtils;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.user.domain.UpdateUserLogin;
import com.spzx.user.domain.UserAddress;
import com.spzx.user.mapper.UserAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserInfoMapper;
import com.spzx.user.domain.UserInfo;
import com.spzx.user.service.IUserInfoService;

/**
 * 会员Service业务层处理
 *
 * @author atguigu
 * @date 2024-06-29
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    /**
     * 查询会员列表
     *
     * @param userInfo 会员
     * @return 会员
     */
    @Override
    public List<UserInfo> selectUserInfoList(UserInfo userInfo) {
        String beginTime = (String) userInfo.getParams().get("beginTime");
        String endTime = (String) userInfo.getParams().get("endTime");
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(userInfo.getUsername()), UserInfo::getUsername, userInfo.getUsername())
                .or().like(StringUtils.isNotEmpty(userInfo.getUsername()), UserInfo::getNickName, userInfo.getUsername())
                .or().like(StringUtils.isNotEmpty(userInfo.getUsername()), UserInfo::getPhone, userInfo.getUsername());
        wrapper.gt(StringUtils.isNotEmpty(beginTime), UserInfo::getCreateTime, beginTime);
        wrapper.lt(StringUtils.isNotEmpty(endTime), UserInfo::getCreateTime, endTime);
        return this.list(wrapper);
    }

    /**
     * 查询会员地址信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserAddress> getUserAddress(Long userId) {
        return Db.lambdaQuery(UserAddress.class).eq(UserAddress::getUserId, userId).list();
    }

    @Override
    public void register(UserInfo userInfo) {
        UserInfo user = this.getOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getUsername, userInfo.getUsername()));
        if (user != null) {
            if (user.getStatus() == Integer.valueOf(UserConstants.USER_DISABLE)) {
                throw new ServiceException("该用户名的账号处于封禁状态，不允许重新注册");
            } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
                userInfo.setDelFlag(UserStatus.OK.getCode());
                this.update(userInfo,
                        new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUsername, userInfo.getUsername()));

            } else {
                throw new ServiceException("该用户已存在");
            }
        }
        userInfo.setPassword(SecurityUtils.encryptPassword(userInfo.getPassword()));
        this.save(userInfo);
    }

    @Override
    public UserInfo selectUserByUserName(String username) {
        return this.getOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUsername, username));
    }

    @Override
    public Boolean updateUserLogin(UpdateUserLogin updateUserLogin) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(updateUserLogin.getUserId());
        userInfo.setLastLoginIp(updateUserLogin.getLastLoginIp());
        userInfo.setLastLoginTime(updateUserLogin.getLastLoginTime());
        this.updateById(userInfo);
        return true;
    }

}
